/***
 *
 * 一个用于解析裁剪图片的类 parsePic.js
 *
 * Daienl
 *
 * 2018.08.14
 *
 * **/

define(function () {
    var scope;
    var picture = function () {
        scope = this;
        this.group = [];
        this.picture = {};
        this.pictureArray = [];
        this.pram = {
            width : 20000,
            height : 20000,
            map: null,
            position: new THREE.Vector3(),
            rotation: new THREE.Vector3()
        };
        this.model = {};
        this.originalData = {
            id:[],
            idgroup:{},
        }
    };
    
    //json数据初始化
    picture.prototype.init = function(json){
        if(json == undefined)return;
        var data = json.data;
        var i =0,len = data.length;
        var loader = new THREE.TextureLoader();
        for(;i<len;i++){
            var porject = data[i];
            var url = porject.img;
            var name = porject.name;
            var id = porject.id;
            var link = porject.keyLink;
            var size = porject.size;
            var p_r = {
                pop:porject.position,
                rot:porject.rotation
            };
            loader.load(url, function (text,fun){
                if(size.w !== undefined){
                    text.wrapS = size.w;
                    text.wrapT = size.h;
                }else{
                    text.wrapS = scope.pram.width;
                    text.wrapT = scope.pram.height;
                }
                
                if(p_r.pop !== undefined && p_r.rot!==undefined){
                    scope.pram.position = p_r.pop;
                    scope.pram.rotation = p_r.rot;
                }
                    scope.pram.map = text;
                var plane = createPlane(scope.pram);
                    plane.name = name;
                    plane.id = id;
                    plane.link = link;
                    plane.url = url;
                    plane.dwg = "DWG";
                    scope.picture["" + text.name + ""] = plane;
                    scope.pictureArray.push(plane);
                if(fun!==undefined)fun(plane);
                    scope.pram.position = new THREE.Vector3();
                    scope.pram.rotation = new THREE.Vector3();
                    scope.pram.map = null;
            });
        }
        
    };
    
    //读取图纸属性
    picture.prototype.get = function(obj){
        if(obj.dwg == undefined)return;
        scope.model = obj;
        var id = obj.id;
        var object = obj.clone();
        var plane = {};
        if(scope.originalData.id.indexOf(id)==-1){
            scope.originalData.idgroup[""+id+""] = object;
            scope.originalData.id.push(id);
        }
        return copy(plane,object);
    };
    
    //设置图形属性
    picture.prototype.set = function(object){
        if(object == undefined || scope.model ==undefined)return;
        var proj = scope.model;
            copy(proj,object);
    };
    
    //保存图形属性
    picture.prototype.save = function(object){
        if(object.dwg ==undefined)return;
        var json = {};
        json.name = object.name;
        json.id = object.id;
        json.keyLink = object.link;
        json.path = object.url;
        json.dwg = object.dwg;
        json.position = object.position;
        json.rotation = object.rotation;
        json.size = {
            w:object.child[0].material.map.wrapS,
            h:object.child[0].material.map.wrapT,
        };
        json.scale = object.scale;
        var datas = JSON.stringify(json);
        return datas;
    };
    
    //单个恢复属性恢复
    picture.prototype.cancel = function(model,name){
        if(model.dwg ==undefined)return;
        scope.model = model;
        var id = model.id;
        var tag = scope.originalData.id.indexOf(id);
        if( tag !==-1){
            var obj = scope.originalData.idgroup[""+id+""].clone();
                model[""+name+""] = obj[""+name+""];
                scope.set(model);
            return scope.get(model);
        }
        
    };
    
    //恢复图形默认属性
    picture.prototype.original = function (object) {
        if(object.dwg == undefined)return;
        scope.model = object;
        var id = object.id;
        var tag = scope.originalData.id.indexOf(id);
        if( tag !==-1){
            var obj = scope.originalData.idgroup[""+id+""].clone();
            delete scope.originalData.idgroup[""+id+""];
            scope.originalData.id.splice(tag,1);
            scope.set(obj);
            return scope.get(obj);
        }
    };
    
    //创建图元
    function createPlane(prams){
        var width = prams.width;
        var height = prams.height;
        var map = prams.map;
        var position = prams.position;
        var rotation = prams.rotation;
        var geometry = new THREE.PlaneGeometry( width, height);
        var material = new THREE.MeshBasicMaterial( {map: map,side:2} );
        
        //通道设置
        //material.opacity = 0.9;
        material.alphaMap = map;
        material.transparent = true;
        material.needsUpdate = true;
        
        var plane = new THREE.Mesh( geometry, material );
        //plane.position.set(position.x,position.y,position.z);
        var group = new THREE.Group();
        
        group.add(plane);
        
        plane.rotation.set(-0.5*Math.PI,0,0);
        group.rotation.set(rotation.x,rotation.y,rotation.z);
        group.position.set(position.x,position.y,position.z);
        return group;
    }
    
    //内部属性快捷赋值
    function copy(plane,object) {
        plane.name = object.name;
        plane.id = object.id;
        plane.link = object.link;
        plane.url = object.url;
        plane.dwg = object.dwg;
        plane.position = object.position;
        plane.rotation = object.rotation;
        plane.width = object.child[0].material.map.wrapS;
        plane.height = object.child[0].material.map.wrapT;
        scope.model = null;
        return plane;
    }
    
    return picture;
    
});